package Dao;

import Bean.Register;
import Bean.User;
import Utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-09
 * Time: 13:15
 */
public class RegisterDao {
    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;


    //1.新增注册申请
    public boolean insert(Register register) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into register values(?,?,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,register.getRegister_id());
            statement.setString(2,register.user.getUserId());
            statement.setString(3,register.user.getUimage());
            statement.setString(4,register.user.getName());
            statement.setString(5,register.user.getPhone());
            statement.setString(6,register.user.getPassword());
            statement.setString(7,register.user.getIdentityCard());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }


    //2.查询所有注册申请
    public List<Register> select (int pageNo,int pageSize) {
        List<Register> array = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from register LIMIT ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,(pageNo-1)*pageSize);
            statement.setInt(2,pageSize);
            resultSet = statement.executeQuery();
            if(resultSet != null) {
                array = new ArrayList<>();
                while(resultSet.next()) {
                    Register register = new Register();
                    User user = new User();
                    register.setRegister_id(resultSet.getString("register_id"));
                    user.setUserId(resultSet.getString("user_id"));
                    user.setUimage(resultSet.getString("uimage"));
                    user.setName(resultSet.getString("name"));
                    user.setPhone(resultSet.getString("phone"));
                    user.setPassword(resultSet.getString("password"));
                    user.setIdentityCard(resultSet.getString("identity_card"));
                    register.user = user;
                    array.add(register);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return array;
    }

    //获取申请列表的数目
    public int size () {
        int size = 0;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "SELECT COUNT(*) AS register_count FROM register";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                size = resultSet.getInt("register_count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return size;
    }

    //3.删除申请数据
    public boolean delete (String registerId) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼装sql语句
            String sql = "delete from register where register_id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,registerId);
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //4.查询出registerId对应的user数据
    public User selectUser(String registerId) {
        User user = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from register where register_id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,registerId);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                user = new User();
                user.setUserId(resultSet.getString("user_id"));
                user.setName(resultSet.getString("name"));
                user.setPhone(resultSet.getString("phone"));
                user.setUimage(resultSet.getString("uimage"));
                user.setPassword(resultSet.getString("password"));
                user.setIdentityCard(resultSet.getString("identity_card"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return user;
    }
}
